package com.topera.educenter.controller;

import com.topera.commonutils.JwtUtils;
import com.topera.commonutils.R;
import com.topera.commonutils.orderVo.UcenterMemberOrder;
import com.topera.educenter.entity.RegisterVo;
import com.topera.educenter.entity.UcenterMember;
import com.topera.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录
     * @param member
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    /**注册
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 根据JWT获取用户信息
     * @param request 
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){

        //调用JWT工具类中的方法
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);

        return R.ok().data("userInfo",member);

    }

    /**
     * 根据用户id获取用户信息
     * @param id
     * @return
     */
    @PostMapping("getUserInforOrder/{id}")
    public UcenterMemberOrder getUserInforOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,memberOrder);
        return memberOrder;
    }

    /**
     * 查询某一天的注册人数--统计分析调用
     * @param day
     * @return
     */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        int registerNum = memberService.countRegister(day);
        return R.ok().data("registerNum",registerNum);
    }
}

